'''
Contains the functions needed to get confirmation for actions in the
GUI applications.
'''
import PySimpleGUI as sg
from warranty_manager.claim.common import SUBMIT_COLOR, CANCEL_COLOR


def get_confirmation(action_to_confirm):
    '''
    Provides a generic confirmation prompt for any kind of action.
    '''
    button_size = (5, 1)

    layout = [
        [sg.T(f'Are you sure you want to {action_to_confirm}?')],
        [
            sg.Button('Yes', size=button_size, button_color=SUBMIT_COLOR),
            sg.Button('No', size=button_size, button_color=CANCEL_COLOR)
        ]
    ]

    window = sg.Window('Confirmation', layout, keep_on_top=True)

    event = window.read()[0]
    window.close()

    if event == 'Yes':
        return True

    return False


def get_csn_confirmation(claim_info):
    '''
    Provides a confirmation prompt to create a new CSN claim.
    '''
    layout = [
        [
            sg.Text(
                f'Are you sure you want to create a new CSN claim, '
                'with the following information?',
                justification='center'
            )
        ],
        [sg.Text(f"PO Number: {claim_info['po number']}")],
        [sg.Text(f"HP Learner ID: {claim_info['learner id']}")],
        [sg.Text(f"Serial Number: {claim_info['serial number']}")],
        [sg.Text(f"Product Number: {claim_info['product number']}")],
        [sg.Text(f"CSN Ship Name: {claim_info['ship name']}")],
        [sg.Text(f"CSN End User: {claim_info['end user']}")],
        [sg.Text(f"Part Numbers: {claim_info.get('part numbers')}")]
    ]

    # To add each note on a separate line
    for i, note in enumerate(claim_info['notes']):
        layout.append([sg.Text('_' * 65)])
        layout.append([sg.Text(f'Note #{i + 1}')])
        layout.append([sg.Text( note, size=(60, None) )])

    layout.append([
        sg.Button('Yes', key='-YES-', button_color=SUBMIT_COLOR),
        sg.Button('No', key='-NO-', button_color=CANCEL_COLOR)
    ])

    window = sg.Window('CSN Confirmation Window', layout, keep_on_top=True)

    event = window.read()[0]
    window.close()

    if event == '-YES-':
        return True
    elif event == '-NO-':
        return False


def get_dell_request_confirmation(request_info, sandbox=False):
    '''
    Provides a confirmation prompt to create a new Dell Self-Dispatch
    support request.
    '''
    window_width = 45

    layout = [
        [
            sg.T(
                'Are you sure you want to create a new Self-Dispatch\n'
                'support request, with the following information?'
            )
        ],
        [sg.T('_' * window_width)],
        [sg.T('Service Tag:'), sg.T(request_info['tag'], text_color='#4ca3dd')],
    ]

    # The address name has been entered
    if len(request_info['address']) == 1:
        layout += [
            sg.T('Address Name:'),
            sg.T(request_info['address']['name'], text_color='#4ca3dd')
        ]
    # The address information has been entered
    else:
        layout += [
            [sg.T('Country ISO Code'), sg.T(request_info['address']['iso'], text_color='#4ca3dd')],
            [sg.T('City'), sg.T(request_info['address']['city'], text_color='#4ca3dd')],
            [sg.T('State'), sg.T(request_info['address']['state'], text_color='#4ca3dd')],
            [sg.T('Zip Code'), sg.T(request_info['address']['zip'], text_color='#4ca3dd')],
            [sg.T('Address Line 1'), sg.T(request_info['address']['line 1'], text_color='#4ca3dd')],
            [sg.T('Address Line 2'), sg.T(request_info['address']['line 2'], text_color='#4ca3dd')],
            [sg.T('Time Zone'), sg.T(request_info['address']['zone'], text_color='#4ca3dd')],
        ]

    # To add the rest of the request fields, except each part number
    layout += [
        [sg.Text('Phone Number:'), sg.Text(request_info['phone'], text_color='#4ca3dd')],
        [sg.Text('PO Number:'), sg.Text(request_info['po'], text_color='#4ca3dd')],
        [sg.Text('Note:')],
        [sg.MLine(default_text=request_info['note'], size=(window_width, 10), disabled=True)],
        [sg.Text(f"Part Numbers:")]
    ]

    # To get each part number and PPID without the list brackets or curly braces
    for part_dict in request_info['parts']:
        layout.append(
            [
                sg.Text(
                    f"P/N: {part_dict['PN']}, PPID: {part_dict['PPID']}",
                    relief=sg.RELIEF_SUNKEN,
                    background_color="#eeeeee",
                    border_width=2
                )
            ]
        )

    # To color the drop ship, ADP, and sandbox booleans, according to their status
    if request_info['drop ship'] == 'true':
        text_color = '#55b54c'
    else:
        text_color = '#ff4040'

    layout.append(
        [
            sg.Text('Drop-Ship:'),
            sg.Text(request_info['drop ship'], text_color=text_color)
        ]
    )

     # Accidental Damage Claim
    if request_info['adp'] == 'true':
        text_color = '#55b54c'
    else:
        text_color = '#ff4040'

    layout.append(
        [
            sg.Text('Accidental Damage Claim:'),
            sg.Text(request_info['adp'], text_color=text_color)
        ]
    )

    # Sandbox
    if sandbox is True:
        text_color = '#55b54c'
    else:
        text_color = '#ff4040'

    layout.append(
        [sg.Text('Sandbox:'), sg.Text(sandbox, text_color=text_color)]
    )

    # To add the Yes and No buttons
    layout.append(
        [
            sg.Button('Yes', key='-YES-', button_color=SUBMIT_COLOR),
            sg.Button('No', key='-NO-', button_color=CANCEL_COLOR)
        ]
    )

    window = sg.Window('Dell Confirmation Window', layout, keep_on_top=True)

    event = window.read()[0]
    window.close()

    if event == '-YES-':
        return True
    elif event == '-NO-':
        return False
